package com.xizi.秋招笔试;

/**
 * @author xizizzz
 * @description: TODO
 * @date 2021-8-11下午 07:25
 */
public class I {


    static class ListNode {
        int val;
        ListNode next = null;

        public ListNode(int val) {
            this.val = val;
        }
    }


    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        ListNode res = head;
        for (int i = 2; i <= 5; i++) {
            ListNode node = new ListNode(i);
            head.next = node;
            head = head.next;
        }
        ListNode res2 = formatList(res);
        while (res2 != null) {
            System.out.println(res2.val);
            res2 = res2.next;
        }
    }

    static ListNode formatList(ListNode head) {
        ListNode res = new ListNode(head.val);
        int index = 0;
        ListNode head1 = res;
        ListNode tail1 = res;
        head = head.next;
        while (head != null) {
            ListNode temp = new ListNode(head.val);
            if (index % 2 == 0) {
                tail1.next = temp;
                tail1 = temp;
            } else {
                temp.next = head1;
                head1 = temp;
            }
            head = head.next;
            index++;
        }
        return head1;
    }
}
